package com.lei.chinese_herbal_medicine.Controller;


import com.lei.chinese_herbal_medicine.Entity.Herb;
import com.lei.chinese_herbal_medicine.Entity.PageData;
import com.lei.chinese_herbal_medicine.Entity.Result;
import com.lei.chinese_herbal_medicine.Service.HerbService;
import com.lei.chinese_herbal_medicine.Util.UUIDUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Herb)表控制层
 *
 * @author makejava
 * @since 2024-01-18 10:05:26
 */
@RestController
public class HerbController {
    /**
     * 服务对象
     */
    @Resource
    private HerbService herbService;
    @Resource
    private Result result;

    @PostMapping("updateHerb")
    public Result updateHerb(MultipartFile file,Herb herb) throws IOException {
        Map map=new HashMap();
        herb.setImg(null);
        if(file!=null&&!file.isEmpty()){
            herb.setImg(file.getBytes());
        }
        if (herbService.updateByPrimaryKeyWithBLOBs(herb)){
            result.setCode(200);
            map.put("message","修改成功");
        }else{
            result.setCode(403);
            map.put("message","修改失败");
        }
        result.setData(map);
        return result;
    }

    @GetMapping("getHerbById")
    public Result getHerbById(String id){
        Herb herb = herbService.selectByPrimaryKey(id);
        Map map=new HashMap();
        if (herb!=null){
            result.setCode(200);
            map.put("message","查询成功");
            map.put("herb",herb);
        }else{
            result.setCode(403);
            map.put("message","该药材不存在获取其他原因，查询失败！");
        }
        result.setData(map);
        return result;
    }
    @DeleteMapping("/deleteHerb")

    public Result deleteHerb(@RequestBody Herb herb) {
        Map map=new HashMap();
        boolean flag=true;
        try{
            flag=herbService.deleteHerbById(herb.getId());
        }catch (RuntimeException e){
            flag=false;
            System.out.println(e.getMessage());
        }
        if(flag){
            result.setCode(200);
            map.put("message","删除成功");
        }else{
            result.setCode(403);
            map.put("message","该中草药不存在或其他原因，删除失败！");
        }
        result.setData(map);
        return result;
    }

    @GetMapping("/getHerbList")
    public Result getHerbList(Herb herb, PageData pageData){
        List<Herb> list = herbService.selectHerbsByHerbAndPageData(herb,pageData);
        result.setCode(200);
        Map map=new HashMap();
        map.put("message","查询成功");
        map.put("pageData",pageData);
        result.setList(list);
        result.setData(map);
        return result;
    }

    /**
     * 添加中草药
     * @param file 中草药图片
     * @param herb 中草药信息
     * @return 是否添加成功
     * @throws IOException
     */
    @PostMapping("/addHerb")
    public Result addHerb(MultipartFile file,Herb herb) throws IOException {
        herb.setId(UUIDUtil.fastSimpleUUID());
        herb.setImg(file.getBytes());
        boolean flag=herbService.insertHerb(herb);
        if(flag){
            result.setCode(200);
            Map map=new HashMap();
            map.put("message","添加成功");
        }else{
            result.setCode(403);
            Map map=new HashMap();
            map.put("message","添加失败");
        }
        return result;
    }
}

